package com.yxhl.zoume.core.user.ui.fragment.passenger;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.AddPassengerActivity;
import com.yxhl.zoume.common.ui.custom.QuicklyClearEditText;
import com.yxhl.zoume.common.ui.event.AddPassengerEvent;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.user.presenter.passenger.AddPassengerPresenter;
import com.yxhl.zoume.core.user.view.passenger.AddPassengerView;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.data.http.rest.param.passenger.PassengerParam;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.usercenter.DaggerPassengerComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.usercenter.PassengerModule;
import com.yxhl.zoume.utils.IDNoUtils;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.RegexUtils;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 添加乘客界面
 */
@SuppressWarnings("ALL")
public class AddPassengerFragment extends BaseFragment implements AddPassengerView {

    private static final String TAG = "AddPassengerFragment";

    @BindView(R.id.frame_passenger_add_root)
    FrameLayout mRootLayout;

    @BindView(R.id.et_include_name)
    QuicklyClearEditText mPassengerName;

    @BindView(R.id.et_include_id_card)
    QuicklyClearEditText mPassengerCardID;

    @BindView(R.id.et_include_mobile)
    QuicklyClearEditText mPassengerMobile;

    @BindView(R.id.rg_include_ticket_group)
    RadioGroup mTicketGroup;

    @BindView(R.id.rb_include_adult)
    RadioButton mAdult;

    @BindView(R.id.rb_include_child)
    RadioButton mChild;

    @Inject
    AddPassengerPresenter mAddPassengerPresenter;
    private PassengerParam mPassengerParam;


    public AddPassengerFragment() {
        // Required empty public constructor
    }

    public static AddPassengerFragment newInstance() {
        return new AddPassengerFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initializeInjector();
        mAddPassengerPresenter.attachView(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        mAddPassengerPresenter.onDestroy();
        super.onDestroyView();
    }

    /**
     * 确认修改乘客信息
     */
    public void goToConfirm() {
        boolean enableConfirm = validPassengerData(mPassengerName, mPassengerCardID,
                mPassengerMobile, mTicketGroup, mAdult, mChild);
        if (enableConfirm) {
            mPassengerParam = getAddPassengerParam();
            mAddPassengerPresenter.confirmAddPassenger(mPassengerParam);
        }
    }

    /**
     * 数据校验
     *
     * @return
     */
    public boolean validPassengerData(TextView passengerNameTv,
                                      TextView passengerCardIDTv, TextView passengerMobileTV,
                                      RadioGroup ticketGroup, RadioButton adultRadio, RadioButton childRadio
    ) {
        String name = passengerNameTv.getText().toString().trim();
        String IDCard = passengerCardIDTv.getText().toString().trim();
        String mobileNumber = passengerMobileTV.getText().toString().trim();
        int radioButtonId = ticketGroup.getCheckedRadioButtonId();

        if (TextUtils.isEmpty(name)) {
            printHint(getString(R.string.update_passenger_name_alert));
            return false;
        } else {
            if (name.length() > 5) {
                printHint(getString(R.string.update_passenger_name_alert_max));
                return false;
            }
        }

        if (TextUtils.isEmpty(IDCard)) {
            printHint(getString(R.string.update_passenger_id_card_alert));
            return false;
        }

        if (TextUtils.isEmpty(mobileNumber)) {
            printHint(getString(R.string.update_passenger_mobile_alert));
            return false;
        }

        /** !!!注意取反 */
        if (!IDNoUtils.isValidIDNo(IDCard)) {
            printHint(getString(R.string.include_passenger_id_card_error_format));
            return false;
        }

        /** !!!注意取反 */
        if (!RegexUtils.isValidMobile(mobileNumber)) {
            printHint(getString(R.string.update_passenger_invalid_mobile_alert));
            return false;
        }

        if (!(childRadio.getId() == radioButtonId || adultRadio.getId() == radioButtonId)) {
            printHint(getString(R.string.update_passenger_ticket_alert));
            return false;
        }

        return true;
    }

    private void printHint(String msg) {
        PromptUtils.showShort(mActivity, msg);
    }

    /**
     * 新增的乘客信息 请求参数
     *
     * @return
     */
    private PassengerParam getAddPassengerParam() {
        PassengerParam passengerParam = new PassengerParam(1);
        String name = mPassengerName.getText().toString().trim();
        String IDCard = mPassengerCardID.getText().toString().trim();
        String mobileNumber = mPassengerMobile.getText().toString().trim();

        passengerParam.setRealname(name);
        passengerParam.setIdNumber(IDCard);
        passengerParam.setMobile(mobileNumber);

        int radioButtonId = mTicketGroup.getCheckedRadioButtonId();
        switch (radioButtonId) {
            case R.id.rb_include_adult:
                passengerParam.setContractType(ZMPassenger.ADULT);
                break;
            case R.id.rb_include_child:
                passengerParam.setContractType(ZMPassenger.CHILD);
                break;
        }

        return passengerParam;
    }


    @Override
    public void renderDataOnSuccess(ZMPassenger zmPassenger) {
        PromptUtils.showShort(mActivity, getString(R.string.add_passenger_info_success));
        RxBus.getInstance().send(new AddPassengerEvent(zmPassenger));

        mActivity.finish();
        mActivity.overridePendingTransition(R.anim.noanimation, R.anim.right_out);
    }

    @Override
    public void renderDataOnError() {
        PromptUtils.showShort(mActivity, getString(R.string.add_passenger_info_error));
    }

    @Override
    public void renderDataOnFailure(String errorMsg) {
        PromptUtils.showShort(mActivity, errorMsg);
    }

    @Override
    protected void setupToolBar() {
        if (mActivity instanceof AddPassengerActivity) {
            AddPassengerActivity addPassengerActivity = (AddPassengerActivity) mActivity;
            addPassengerActivity.setToolbar(getString(R.string.add_passenger), getString(R.string.confirm));
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_add_passenger;
    }

    protected void initializeInjector() {
        DaggerPassengerComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(getContext()))
                .passengerModule(new PassengerModule())
                .build().inject(this);
    }
}
